#include <logger.h>
#include <napi_call.h>
#include <node_api.h>

struct PropData {
    napi_value val = nullptr;
    const char *utf8name = "unknown";
};
PropData props[] = {
    {.utf8name = "array"},
    // {.utf8name = "array_with_length"},
    // {.utf8name = "arraybuffer"},
    // {.utf8name = "buffer"},
    // {.utf8name = "buffer_copy"},
    {.utf8name = "date"},
    // {.utf8name = "external"},
    // {.utf8name = "external_arraybuffer"},
    // {.utf8name = "external_buffer"},
    {.utf8name = "object"},
    // {.utf8name = "symbol"},
    // {.utf8name = "typedarray"},
    // {.utf8name = "dataview"},
    // {.utf8name = "global"},
    // {.utf8name = "null"},
    // {.utf8name = "undefined"},
    // {.utf8name = "int32"},
    // {.utf8name = "uint32"},
    // {.utf8name = "int64"},
    // {.utf8name = "double"},
    // {.utf8name = "bigint_int64"},
    // {.utf8name = "bigint_uint64"},
    // {.utf8name = "bigint_words"},
    // {.utf8name = "string_latin1"},
    // {.utf8name = "string_utf16"},
    // {.utf8name = "string_utf8"},
    {.utf8name = "boolean"},
};

/* napi_value */
NAPI_MODULE_INIT(/* napi_env env, napi_value exports */)
{
    int index = 0;

    NAPI_CALL(env, napi_create_array_with_length(env, 13, &props[index++].val));
    NAPI_CALL(env, napi_create_date(env, 10, &props[index++].val));
    NAPI_CALL(env, napi_create_object(env, &props[index++].val));
    NAPI_CALL(env, napi_get_boolean(env, false, &props[index++].val));

    index = 0;
    uint32_t arrlen = 0;
    NAPI_CALL(env, napi_get_array_length(env, props[index++].val, &arrlen));
    VALUE_LOGGER(arrlen);
    // arrlen: 13

    double dateVal = 0;
    NAPI_CALL(env, napi_get_date_value(env, props[index++].val, &dateVal));
    VALUE_LOGGER(dateVal);
    // dateVal: 10

    napi_value proto = nullptr;
    NAPI_CALL(env, napi_get_prototype(env, props[index++].val, &proto));
    napi_value protoStr = nullptr;
    NAPI_CALL(env, napi_coerce_to_string(env, proto, &protoStr));
    char protoStrBuf[32] = {};
    size_t protoStrBufLen = 0;
    NAPI_CALL(env, napi_get_value_string_utf8(env, protoStr, protoStrBuf, sizeof(protoStrBuf), &protoStrBufLen));
    LOG2SO(DEBUG) << "[" << protoStrBufLen << "]: '" << protoStrBuf << "'";
    // [15]: '[object Object]'

    bool boolVal = true;
    NAPI_CALL(env, napi_get_value_bool(env, props[index++].val, &boolVal));
    VALUE_LOGGER(boolVal);
    // boolVal: false

    return exports;
}
